<?php

/**
 * Ignus Framework
 *
 * @author Sergey Palyanov <sergey.palyanov@gmail.com>
 * @link http://code.google.com/p/ignus-php/
 * @license http://apache.org/licenses/LICENSE-2.0
 * @package Ignus
 * @subpackage Log
 * @since 0.1
 */

namespace Ignus\Log\Append;

use Ignus\App;
use Ignus\Log\Logger;

/**
 * @package Ignus
 * @subpackage Log
 * @since 0.1
 */
class FileAppender implements Appender
{
    /**
     * Logging strings
     * @var array
     */
    private $entries = array();

    /**
     * @param array $entries
     * @return void
     */
    public function append(array $entry)
    {
        $this->entries[] = sprintf("%s,%03d %5s [%s] %s\n",
            date('Y-m-d H:i:s', $entry[Logger::ENTRY_SECONDS]),
            $entry[Logger::ENTRY_MSEC] * 1000,
            $entry[Logger::ENTRY_LEVEL], $entry[Logger::ENTRY_NAME],
            $entry[Logger::ENTRY_MESSAGE]);
    }

    /**
     * Finalize appender
     * @since 0.5
     */
    public function finalize()
    {
        if (!count($this->entries))
            return;

        $filename = App::getPath('logs', 'ignus.' . date('Y-m-d') . '.log');
        file_put_contents($filename, $this->entries, FILE_APPEND);
    }
}
